//
//  ZSQHttpmanager.m
//  2-QiuBai
//
//  Created by qianfeng on 16/5/6.
//  Copyright © 2016年 xuzhixiang1. All rights reserved.
//

#import "ZSQHttpmanager.h"

@interface ZSQHttpmanager ()

@property (nonatomic,strong) AFHTTPRequestOperationManager *afHttpManager;
@property (nonatomic,strong) AFHTTPRequestOperationManager *xmlHttpManager;
@property (nonatomic,strong) AFHTTPSessionManager *xmlSessionManager;
@property (nonatomic,strong) AFHTTPSessionManager *afHttpSessionManager;
@end

@implementation ZSQHttpmanager

+ (instancetype)sharedManager {
    static ZSQHttpmanager *manager = nil;
    static dispatch_once_t tokenToken;
    dispatch_once(&tokenToken, ^{
        /*********/
        manager = [[ZSQHttpmanager alloc] init];
        manager.afHttpManager = [AFHTTPRequestOperationManager manager];
        
        manager.xmlHttpManager = [AFHTTPRequestOperationManager manager];
        manager.xmlHttpManager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        manager.xmlSessionManager = [AFHTTPSessionManager manager];
        manager.xmlSessionManager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        
        NSMutableSet *set = [NSMutableSet setWithSet:manager.afHttpManager.responseSerializer.acceptableContentTypes];
        [set addObject:@"text/html"];
        [set addObject:@"text/xml"];
        manager.afHttpManager.responseSerializer.acceptableContentTypes = set;
        
////        manager = [[ZSQHttpmanager alloc] init];
        manager.afHttpSessionManager = [AFHTTPSessionManager manager];
        
        NSMutableSet *set1 = [NSMutableSet setWithSet:manager.afHttpManager.responseSerializer.acceptableContentTypes];
        [set1 addObject:@"text/html"];
        [set1 addObject:@"text/xml"];
        manager.afHttpSessionManager.responseSerializer.acceptableContentTypes = set;
    });
    return manager;
}
/**********/
- (void)GET:(NSString *)url param:(NSDictionary *)params success:(SuccessBlock)success failure:(Failure)failure {
    [_afHttpManager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //请求成功以后回调
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //请求失败回调
        failure(operation,error);
    }];
}
- (void)POST:(NSString *)url param:(NSDictionary *)params success:(SuccessBlock)success failure:(Failure)failure {
    [_afHttpManager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //求成成功的回调
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation,error);
    }];
}

- (void)GET:(NSString *)url parameters:(NSDictionary *)params success:(SuccessBlockSession)success failure:(FailureSession)failure {
    [_afHttpSessionManager GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        //请求成功以后回调
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //失败的回调
        failure(task,error);
    }];
}
- (void)POST:(NSString *)url parameters:(NSDictionary *)params success:(SuccessBlockSession)success failure:(FailureSession)failure {
    [_afHttpSessionManager POST:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        //请求成功以后回调
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //失败的回调
        failure(task,error);
    }];
}



- (void)GETXML:(NSString *)url param:(NSDictionary *)params success:(SuccessBlock)success failure:(Failure)failure {
    [_xmlHttpManager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //请求成功以后回调
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //请求失败回调
        failure(operation,error);
    }];
}
- (void)POSTXML:(NSString *)url param:(NSDictionary *)params success:(SuccessBlock)success failure:(Failure)failure {
    [_xmlHttpManager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //求成成功的回调
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation,error);
    }];
}

- (void)GETXML:(NSString *)url parameters:(NSDictionary *)params success:(SuccessBlockSession)success failure:(FailureSession)failure {
    [_xmlSessionManager GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        //请求成功以后回调
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //失败的回调
        failure(task,error);
    }];
    
}
- (void)POSTXML:(NSString *)url parameters:(NSDictionary *)params success:(SuccessBlockSession)success failure:(FailureSession)failure {
    [_xmlSessionManager POST:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        //请求成功以后回调
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //失败的回调
        failure(task,error);
    }];
}

@end
